#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "mbedtls/md5.h"
#include "mbedtls/md.h"

#include "aliot.h"
#include "nvs_manager.h"

#define TAG     "aliot"

//设备秘钥
static char s_device_secret[128] = {0};

/**
 * 获取设备名称
 * @param 无
 * @return 设备名称
 */
char* aliot_get_devicename(void)
{
    static char aliot_mqtt_devicename[64] = {0};

    if (aliot_mqtt_devicename[0] == 0) {
        nv_aliot_info_t aliot_info = {0};

        // nvs_aliot_info_read(&aliot_info);
        snprintf(aliot_mqtt_devicename, sizeof(aliot_mqtt_devicename), "%s", aliot_info.device_name);
        // snprintf(aliot_mqtt_devicename, sizeof(aliot_mqtt_devicename), "%s", NVS_ALIOT_DEVICE_NAME);
    }
    return aliot_mqtt_devicename;
}

/**
 * 获取客户端id
 * @param 无
 * @return 客户端id
 */
char* aliot_get_clientid(void)
{
    return aliot_get_devicename();
}

/**
 * 获取设备秘钥（此秘钥第一次上电需通过动态注册获取）
 * @param 无
 * @return 设备秘钥
 */
char* aliot_get_devicesecret(void)
{
    static char is_first_load = 0;

    if (!is_first_load) {
        nv_aliot_info_t aliot_info = {0};

        //nvs_aliot_info_read(&aliot_info);
        snprintf(s_device_secret, sizeof(s_device_secret), "%s", aliot_info.device_secret);
        // snprintf(s_device_secret, sizeof(s_device_secret), "%s", NVS_ALIOT_DEVICE_SECRET);
        is_first_load = 1;
    }

    return s_device_secret;

}

/**
 * 计算hmd5
 * @param key 秘钥
 * @param content 内容
 * @param output 输出md5值
 * @return 无
 */
void calc_hmd5(char* key, char *content, char *output)
{
    mbedtls_md_context_t md5_ctx;
    const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);

    mbedtls_md_init(&md5_ctx);
    mbedtls_md_setup(&md5_ctx, md_info, 1);
    mbedtls_md_hmac_starts(&md5_ctx, (const unsigned char *)key, strlen(key));
    mbedtls_md_hmac_update(&md5_ctx, (const unsigned char *)content, strlen(content));
    mbedtls_md_hmac_finish(&md5_ctx, (unsigned char *)output);
    mbedtls_md_free(&md5_ctx);
}

/**
 * hex转str
 * @param input 输入
 * @param input_len 输入长度
 * @param output 输出
 * @param lowercase 0:大小，1:小写
 * @return 无
 */
void core_hex2str(char *input, uint32_t input_len, char *output, uint8_t lowercase)
{
    char *upper = "0123456789ABCDEF";
    char *lower = "0123456789abcdef";
    char *encode = upper;
    int i=0,j=0;
    
    // 选择大小写
    if (lowercase) {
        encode = lower;
    }

    for (i=0; i<input_len; i++) {
        output[j++] = encode[(input[i]>>4 & 0xf)];
        output[j++] = encode[(input[i]) & 0xf];
    }
    output[j] = 0;
}

//根证书
const char* g_aliot_ca = "-----BEGIN CERTIFICATE-----\n"
"MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG"
"A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv"
"b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw"
"MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i"
"YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT"
"aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ"
"jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp"
"xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp"
"1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG"
"snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ"
"U26Qzns3dLlwR5EiUWMWea6xrkE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